import { App } from "../../../../src/App";
import { EventConst } from "../../../../src/core/EventConst";
import ViewMgr from "../../../../src/core/ViewMgr";
import { GameModule, LoginType } from "../Game/GameModule";

const { menu, ccclass, property } = cc._decorator;
@ccclass
@menu("斗地主/登陆场景")
export default class LoginScene extends cc.Component {
    @property(cc.Label)
    lblCopyRight: cc.Label = null;
    @property(cc.Label)
    lblVersion: cc.Label = null;
    @property(cc.Node)
    loginNode: cc.Node = null;
    @property(cc.Node)
    hotupdateNode: cc.Node = null;
    @property(cc.Toggle)
    btnRead: cc.Toggle = null;

    start(){
        this.hotupdateNode.active = !App.isLogout;

        this.loginNode.active = App.isLogout;
        this.lblVersion.string = App.Util.format(App.getText('version'),App.getCfg('modules/master/version'));// `${App.getText('version')}${App.getCfg('version') || App.getCfg('modules/master/version')}`;
        this.lblCopyRight && (this.lblCopyRight.string = App.getText('copy_right'));
        this.btnRead.isChecked = true;

        const channels = App.getCfg('channel');
        const channelNode =  App.Util.find<cc.Node>('channel',this.loginNode);
        channelNode.active = channels.length > 0;
        channelNode.children.forEach((btn)=>{
            btn.active = false;
            const btnName = btn.name.toLocaleLowerCase();
            for (let id of channels){
                const typeName = LoginType[id].toLocaleLowerCase();
                if (btnName.indexOf(typeName) !== -1){
                    btn.active = true;
                    break ;
                }
            }
        });

        App.Event.on(EventConst.START_LOGIN,this._startLogin,this);
        !App.isLogout && App.Event.emit(EventConst.CHECK_MODULE,App.MOD_NAME.MASTER);
        GameModule.init();
    }

    onLoginClicket(_,flag:string){ 
        GameModule.login(parseInt(flag || '0'));
    }

    onProtocolClicket(_,flag){
        App.openView(`HtmlView:${App.MOD_NAME.HALL}`,null,App.getCfg(`user_${flag}`),App.getText(`user_${flag}`));
    }

    private _startLogin(isAuto:boolean){
        this.lblVersion.string = App.Util.format(App.getText('version'),App.getCfg('modules/master/version'));
        this.lblCopyRight && (this.lblCopyRight.string = App.getText('copy_right'));
        this.hotupdateNode.active = false;
        this.loginNode.active = true;

        if (!this.btnRead.isChecked){
            App.popTips('read_service_protocol');
            return ;
        }

        if (App.isLogout) return ;

        // 是否自动登陆
        let _local = App.getLocalStorage(App.LOCAL_KEY.LOGIN);
        if (isAuto && _local && _local.lastTime && (_local.loginType > LoginType.Unknow && _local.loginType < LoginType.Max)){   
            let _dates = Math.abs((_local.lastTime - (new Date()).getTime()))/(1000*60*60*24);
            if (_dates < 8){
                GameModule.login(_local.loginType);
            } else {
                _local.lastTime = 0;
                _local.token = '';
                _local.svrUrl = '';
                App.setLocalStorage(_local,App.LOCAL_KEY.LOGIN);
            }
        }
    }
}